package com.lyk.coursearrange.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyk.coursearrange.entity.CoursePlan;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

/**
 * @author xqj
 * @since 2023-03-21
 */
public interface CoursePlanDao extends BaseMapper<CoursePlan> {

    // 将上课计划插入上课计划的表中
//    @Insert("insert into tb_course_plan ")
//    void insertCoursePlan(CoursePlan coursePlan);

    // 插入课程计划

    @Insert("insert into tb_course_plan(grade_no, class_no, course_no, teacher_no, classroom_no, class_time, semester) values(#{grade_no}, #{class_no}, #{course_no}, #{teacher_no}, #{classroom_no}, #{class_time}, #{semester})")
    void insertCoursePlan(@Param("grade_no") String grade_no, @Param("class_no") String class_no, @Param("course_no") String course_no,
                          @Param("teacher_no") String teacher_no, @Param("classroom_no") String classroom_no, @Param("class_time") String class_time, @Param("semester") String semester);

    @Update("truncate tb_course_plan")
    void deleteAllPlan();
    @Options(statementType = StatementType.CALLABLE)
    @Select("call update_course_plan(#{id, mode=IN, jdbcType=INTEGER}, #{classroomNo, mode=IN, jdbcType=VARCHAR}, #{classTime, mode=IN, jdbcType=VARCHAR}, #{message, mode=INOUT, jdbcType=VARCHAR})")
    void updateCoursePlan(@Param("id") Integer id, @Param("classroomNo") String classroomNo, @Param("classTime") String classTime, @Param("message") String message);

    /**
     * 直接插入
     * @param id
     * @param classroomNo
     * @param classTime
     */
    @Options(statementType = StatementType.CALLABLE)
    @Select("call update_course_plan_with_no_check(#{id, mode=IN, jdbcType=INTEGER}, #{classroomNo, mode=IN, jdbcType=VARCHAR}, #{classTime, mode=IN, jdbcType=VARCHAR})")
    void updateCoursePlanWithNoCheck(@Param("id") Integer id, @Param("classroomNo") String classroomNo, @Param("classTime") String classTime);



    public default CoursePlan findDuplicateCoursePlan(Integer id, String classroomNo, String classTime) {
        QueryWrapper<CoursePlan> wrapper = new QueryWrapper<>();
        wrapper.eq("classroom_no", classroomNo)
                .eq("class_time", classTime)
                .eq("deleted", 0);
        return this.selectOne(wrapper);
    }

    void deleteBySemester( @Param("semester") String semester);
    @Select("CALL delete_course_plan_by_semester(#{semester, mode=IN, jdbcType=VARCHAR})")
    void deleteCoursePlanBySemester(@Param("semester") String semester);
}
